package org.spiderflow.core.expression.parsing.enums;

import org.spiderflow.core.expression.ExpressionError;
import org.spiderflow.core.expression.parsing.token.Token;
import org.spiderflow.core.expression.parsing.token.TokenType;

/**
 * @author zhaoshihao
 * @version 2021/11/9 15:40
 */
public enum BinaryOperator {
    /**
     *
     */
    ADDITION,
    /**
     *
     */
    SUBTRACTION,
    /**
     *
     */
    MULTIPLICATION,
    /**
     *
     */
    DIVISION,
    /**
     *
     */
    MODULO,
    /**
     *
     */
    EQUAL,
    /**
     *
     */
    NOT_EQUAL,
    /**
     *
     */
    LESS,
    /**
     *
     */
    LESS_EQUAL,
    /**
     *
     */
    GREATER,
    /**
     *
     */
    GREATER_EQUAL,
    /**
     *
     */
    AND,
    /**
     *
     */
    OR,
    /**
     *
     */
    XOR,
    /**
     *
     */
    ASSIGNMENT,
    ;

    public static BinaryOperator getOperator(Token op) {
        if (op.getType() == TokenType.PLUS) {
            return ADDITION;
        }
        if (op.getType() == TokenType.MINUS) {
            return SUBTRACTION;
        }
        if (op.getType() == TokenType.ASTERISK) {
            return MULTIPLICATION;
        }
        if (op.getType() == TokenType.FORWARD_SLASH) {
            return DIVISION;
        }
        if (op.getType() == TokenType.PERCENTAGE) {
            return MODULO;
        }
        if (op.getType() == TokenType.EQUAL) {
            return EQUAL;
        }
        if (op.getType() == TokenType.NOT_EQUAL) {
            return NOT_EQUAL;
        }
        if (op.getType() == TokenType.LESS) {
            return LESS;
        }
        if (op.getType() == TokenType.LESS_EQUAL) {
            return LESS_EQUAL;
        }
        if (op.getType() == TokenType.GREATER) {
            return GREATER;
        }
        if (op.getType() == TokenType.GREATER_EQUAL) {
            return GREATER_EQUAL;
        }
        if (op.getType() == TokenType.AND) {
            return AND;
        }
        if (op.getType() == TokenType.OR) {
            return OR;
        }
        if (op.getType() == TokenType.XOR) {
            return XOR;
        }
        if (op.getType() == TokenType.ASSIGNMENT) {
            return ASSIGNMENT;
        }
        throw ExpressionError.error("Unknown binary operator " + op + ".", op.getSpan());
    }
}
